package com.atguigu.java;

import org.junit.Test;

import java.io.*;
import java.time.Duration;
import java.time.LocalDateTime;

/**
 *  处理流之一：缓冲流的使用
 *      1.缓冲流：
 *              BufferedInputStream
 *              BufferedOutputStream
 *              BufferedReader
 *              BufferedWriter
 *
 *      2.作用：提供流的读取、写入的速度
 *              提高读写速度的原因：内部提供了一个缓冲区
 *
 *      3.处理流，就是"套接"在已有的流的基础上。
 *
 * @author : leimingchao
 * @date Date : 2021年10月10日 8:48
 */
public class BufferedTest {

    /**
     * 实现非文本文件的复制
     */
    @Test
    public void bufferedStreamTest() {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            //1.造文件
            File srcFile = new File("爱情与友情.jpg");
            File destFile = new File("爱情与友情4.jpg");
            //2.造流
            //2.1 造节点流
            FileInputStream fileInputStream = new FileInputStream(srcFile);
            FileOutputStream fileOutputStream = new FileOutputStream(destFile);
            //2.造缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.复制的细节；读取、写入
            byte[] arr = new byte[10];
            int len;
            while ((len = bufferedInputStream.read(arr)) != -1) {
                bufferedOutputStream.write(arr,0,len);
                //刷新缓冲区
                // bufferedOutputStream.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源,
            // 要求：先关闭外层的流，再关闭内层的流
            if (bufferedOutputStream != null) {

                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {

                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //说明：关闭外层流的同时，内层流也会自动的进行关闭。关于内层流的关闭，我们可以省略
            // fileOutputStream.close();
            // fileInputStream.close();
        }
    }

    /**
     * 实现文件复制的方法
     */
    public void copyFileWithBuffered(String srcPath,String destPath) {
        BufferedInputStream bufferedInputStream = null;
        BufferedOutputStream bufferedOutputStream = null;

        try {
            //1.造文件
            File srcFile = new File(srcPath);
            File destFile = new File(destPath);
            //2.造流
            //2.1 造节点流
            FileInputStream fileInputStream = new FileInputStream(srcFile);
            FileOutputStream fileOutputStream = new FileOutputStream(destFile);
            //2.造缓冲流
            bufferedInputStream = new BufferedInputStream(fileInputStream);
            bufferedOutputStream = new BufferedOutputStream(fileOutputStream);

            //3.复制的细节；读取、写入
            byte[] arr = new byte[10];
            int len;
            while ((len = bufferedInputStream.read(arr)) != -1) {
                bufferedOutputStream.write(arr,0,len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4.关闭资源,
            // 要求：先关闭外层的流，再关闭内层的流
            if (bufferedOutputStream != null) {

                try {
                    bufferedOutputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedInputStream != null) {

                try {
                    bufferedInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //说明：关闭外层流的同时，内层流也会自动的进行关闭。关于内层流的关闭，我们可以省略
            // fileOutputStream.close();
            // fileInputStream.close();
        }
    }

    @Test
    public void testCopyFileWithBuffered() {

        LocalDateTime startDateTime = LocalDateTime.now();
        String srcPath = "爱情与友情.jpg";
        String destPath = "爱情与友情5.jpg";
        copyFileWithBuffered(srcPath,destPath);
        LocalDateTime endDateTime = LocalDateTime.now();

        long seconds = Duration.between(startDateTime, endDateTime).getSeconds();
        System.out.println("复制操作花费的时间：" + seconds * 1000);
    }

    /**
     * 使用BufferedReader和BufferWriter实现文本文件的复制
     *
     */
    @Test
    public void testBufferedReaderBufferWriter() {

        BufferedReader bufferedReader = null;
        BufferedWriter bufferedWriter = null;
        try {
            //创建文件和相应的流
            bufferedReader = new BufferedReader(new FileReader(new File("dbcp.txt")));
            bufferedWriter = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));
            //读写操作
            //方式一：使用的是char数组
            // char[] arr = new char[1024];
            // int len;
            // while ((len = bufferedReader.read(arr)) != -1) {
            //     bufferedWriter.write(arr,0,len);
            //     // bufferedWriter.flush();
            // }
            //方式二：使用String
            String data;
            while ((data = bufferedReader.readLine()) != null) {
                //data中不包含换行符
                //方法一：
                // bufferedWriter.write(data + "\n");
                //方法二：
                //data中不包含换行符
                bufferedWriter.write(data);
                //提供换行的操作
                bufferedWriter.newLine();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (bufferedWriter != null) {

                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {

                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
